<?php
	require("Util.php");
	
	$stat = deleteFiles();
	echo "&result=".$stat;
	
	/*
		Update the book template file by removing all image, newpage, section and h tag
		containing the specified file as its filename
		@param string $input_file the book template file
		@param string $filename the file that is removed from the repository
	*/
	function appendTemplateFile ($input_file, $filename) {
		$str = file_get_contents($input_file);
				
		$testname = $filename;
		$fname = explode(".", $filename);
		if (strcmp(strtolower($fname[1]),"swf") == 0) {
			$testname = strtolower($fname[0]);
		}
		$tag_list = array("section", "newpage", "h[\d]+");
		for ($j = 0; $j < count($tag_list); $j++) {
			$tag = $tag_list[$j];
			preg_match_all('/<'.$tag.'[^>]* filename="([^"]+)"[^>]+>/i', $str, $matches, PREG_SET_ORDER);
			foreach ($matches as $val) {
				$img_list = explode(";", $val[1]);
				$new_img_list = "";
				for ($i = 0; $i < count($img_list); $i++) {
					if (strlen($new_img_list) > 0) {
						$new_img_list = $new_img_list.";";
					}
					if (strcmp(strtolower($img_list[$i]), strtolower($testname)) != 0) {
						$new_img_list = $new_img_list.$img_list[$i];
						$new_count++;
					}
				}
				$new_tag = str_replace("filename=\"".$val[1]."\"", "", $val[0]);
				if (strlen($new_img_list) > 0) {
					$new_tag = str_replace($val[1], $new_img_list, $val[0]);
				} else {
					//need to delete the pagetext when we remove the newpage tag
					$capos = stripos($str,$val[0]);
					while ($capos !== false) {
						$pstart = "<pagetext>";
						$ptest = substr($str, $capos + strlen($val[0]), strlen($pstart));
						if (strcmp(strtolower($ptest), $pstart) == 0) {
							//remove the content of the pagetext
							$pend = "</pagetext>";
							$eapos = stripos($str, $pend, 
															 $capos + strlen($val[0]) + strlen($pstart));
							if ($eapos !== false) {
								$istr = substr($str, 0, $capos);
								$estr = substr($str, $eapos + strlen($pend));
								$str = $istr.$estr;
							}
						}
						$capos = stripos($str,$val[0]);
					}
				}
				$str = str_replace($val[0], $new_tag, $str);
			}
		}
		
		preg_match_all('/<img[^>]* src="'.$filename.'"[^>]+>/i', $str, $matches, PREG_SET_ORDER);
		foreach ($matches as $val) {
			$str = str_replace($val[0], "", $str);
		}
		
		//save the file
		$file = modifyFile($input_file,"w");
		if (!$file) die ("cannot open the file");
		fwrite($file, $str);
		fclose($file);
	}
	
	/*
		Delete all the files in the _POST data
	*/
	function deleteFiles () {
		$folderpath = $GLOBALS['uploaddir'];
		$txt = $_POST["data"];
		
		preg_match_all("/<templateFile>([^<]*)<\/templateFile>/i", $txt, $matches, PREG_SET_ORDER);
		if (count($matches) == 0)
			return "bookID is not specified";
		else {
			foreach ($matches as $val) {
				$folderpath .= "/".$val[1];
			}
		}
		
		$err = "";
		preg_match_all("/<deleteFile>([^<]*)<\/deleteFile>/i", $txt, $matches, PREG_SET_ORDER);
		foreach ($matches as $val) {
			$tstfile = $folderpath."/repository/".$val[1];
			if (file_exists($tstfile)) {
				unlink($tstfile);
				
				$lasttext = substr($val[1], strlen($val[1]) - strlen("@flv.swf"));
				if (strcmp(strtolower($lasttext), "@flv.swf") == 0) {
					$mfile = str_replace("@flv.swf", ".flv", $val[1]);
					unlink($folderpath."/repository/".$mfile);
				} else if (strcmp(strtolower($lasttext), "@mp3.swf") == 0) {
					$mfile = str_replace("@mp3.swf", ".mp3", $val[1]);
					unlink($folderpath."/repository/".$mfile);
				}
				
				appendTemplateFile($folderpath."/".$GLOBALS["book"], $val[1]);
				appendTemplateFile($folderpath."/".$GLOBALS["wikibook"], $val[1]);
			} else {
				$err .= $val[1]." does not exists";
			}
		}
		
		if (strlen($err) == 0)
			$err = "no error";
		
		$toFile = "<p>Uploaded files:</p>\n";
		$toFile = listFiles ($folderpath."/repository/", $toFile);
				
		$process_file = "processed.htm";
		$pfile = modifyFile($folderpath."/".$process_file,"w");
		if (!$pfile) die ("cannot open the file");
		fwrite($pfile, $toFile);
		fclose($pfile);
		
		return $err;
	}
?>